package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {

   //注入DAO对象
    @Autowired
    private CheckItemDao checkItemDao;

//    添加方法
    @Override
    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }


    //根据条件分页查询
    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);

        Page<CheckItem> page =checkItemDao.selectByCondition(queryString);

        long total = page.getTotal();
        List<CheckItem> rows = page.getResult();

        return new PageResult(total,rows);
    }

    //根据id删除检查项
    @Override
    public void delete(Integer id) {
//        System.out.println(id);

//        判断要删除的检查项是都和检查组关联
        Long count = checkItemDao.findCountByCheckItemId(id);
        if (count > 0){
            //当前检查项已经被关联到检查组，不允许删除
            new RuntimeException();
        }
        checkItemDao.deleteById(id);

    }

    //根据id查询数据.完成回显
    @Override
    public CheckItem findById(Integer id) {

        return checkItemDao.findById(id);

    }

    @Override
    public void edit(CheckItem checkItem) {

        checkItemDao.edit(checkItem);

    }

    //查询所有检查项
    @Override
    public List<CheckItem> findAll() {
        return checkItemDao.findAll();
    }


}
